﻿using System;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Web.Security;
using System.Web.SessionState;
using System.Text;
public partial class data_logincheck : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string methodName = Request["method"];
        if (string.IsNullOrEmpty(methodName)) return;

        //invoke method
        Type type = this.GetType();
        MethodInfo method = type.GetMethod(methodName);
        method.Invoke(this, null);
    }

    //登录
    public void Login()
    {
        string UserName = Request["user"];
        string Pwd = Request["pw"];
        string Yzm = Request["yzm"];
        if (Session["yanzhengma"] == null)
        {
            Response.Write("请刷新验证码重试！");
            return;
        }
        if (!Session["yanzhengma"].ToString().ToLower().Equals(Yzm.ToLower()))
        {
            Response.Write("验证码不正确！");
            return;
        }
        if (Session["su_login"] == null)
        {
            string res = "";
            DataTable datatable = new sql().CheckLogin(UserName);
            string pw = FormsAuthentication.HashPasswordForStoringInConfigFile(Pwd, "MD5").ToLower();

            if (datatable.Rows.Count == 0)
                res = "用户名不存在！";//用户名不存在
            else
            {
                if (pw.Equals(datatable.Rows[0]["su_pwd"].ToString().Trim()))
                {
                    if ("0".Equals(datatable.Rows[0]["su_state"].ToString()))
                        res = "用户已被停用！";
                    else
                    {
                        Session["su_no"] = datatable.Rows[0]["su_no"];
                        Session["su_login"] = datatable.Rows[0]["su_login"];
                        Session["su_power"] = datatable.Rows[0]["su_power"];
                        res = "通过";//通过
                        new sql().Login(datatable.Rows[0]["su_no"].ToString(), UserName, Request.UserHostAddress);
                        HttpCookie cookie = new HttpCookie("HZRCWCHTYH");
                        cookie.Values["SystemUser"] = datatable.Rows[0]["su_no"].ToString();
                        cookie.Expires = DateTime.Now.AddDays(1);
                        Response.Cookies.Add(cookie);
                    }
                }
                else
                    res = "密码错误！";//密码错误
            }
            Response.Write(res);
        }
        else
        {
            if (!UserName.Equals(Session["su_login"].ToString()))
                Response.Write("检测到其他用户已登录，请先注销，再登录！");
            else
                Response.Write("通过");
        }
    }

    //登录
    public void Login2()
    {
        string json = Request["data"];
        Hashtable data = (Hashtable)JSON.Decode(json);
        string UserName = data["user"].ToString();
        string Pwd = data["pwd"].ToString();
        if (Session["su_login"] == null)
        {
            string res = "";
            DataTable datatable = new sql().CheckLogin(UserName);
            string pw = FormsAuthentication.HashPasswordForStoringInConfigFile(Pwd, "MD5").ToLower();

            if (datatable.Rows.Count == 0)
                res = "用户名不存在！";//用户名不存在
            else
            {
                if (pw.Equals(datatable.Rows[0]["su_pwd"].ToString().Trim()))
                {
                    if ("0".Equals(datatable.Rows[0]["su_state"].ToString()))
                        res = "用户已被停用！";
                    else
                    {
                        Session["su_no"] = datatable.Rows[0]["su_no"];
                        Session["su_login"] = datatable.Rows[0]["su_login"];
                        Session["su_power"] = datatable.Rows[0]["su_power"];
                        res = "通过";//通过
                        new sql().Login(datatable.Rows[0]["su_no"].ToString(), UserName, Request.UserHostAddress);
                    }
                }
                else
                    res = "密码错误！";//密码错误
            }
            Response.Write(res);
        }
        else
        {
            if (!UserName.Equals(Session["su_login"].ToString()))
                Response.Write("检测到其他用户已登录，请先注销，再登录！");
            else
                Response.Write("通过");
        }
    }

    /// <summary>
    /// 权限树
    /// </summary>
    public void GetMenuTree()
    {
        string id = Request["id"];
        if (string.IsNullOrEmpty(id))
        {
            DataTable datatable = new sql().GetMenuTree();
            string json = JSON.Encode(datatable);
            Response.Write(json);
        }
        else
        {
            DataTable datatable = new sql().GetMenuTree(id);
            string json = JSON.Encode(datatable);
            Response.Write(json);
        }
    }

    /// <summary>
    /// 获取招聘企业名称
    /// </summary>
    public void GetCompanyName()
    {
        string id = Request["id"];
        string name = new sql().GetCompanyName(id);
        Response.Write(name);
    }

    /// <summary>
    /// 获取网站基本信息
    /// </summary>
    public void GetWebset()
    {
        Hashtable webset = new sql().GetWebset();
        string json = JSON.Encode(webset);
        Response.Write(json);
    }
}